home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / Slider.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  7.1 KB  |  299 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.awt.Canvas;
  5. import java.awt.Dimension;
  6.  
  7.  
  8. /**
  9.  * Components based on this class are used to select 
  10.  * one value from a continuous range of values. It has a movable thumb in 
  11.  * front of a gauge with ticks marks on it.
  12.  * <p>
  13.  * @see symantec.itools.awt.HorizontalSlider
  14.  * @see symantec.itools.awt.VerticalSlider
  15.  * @version 1.0, Nov 26, 1996
  16.  * @author Symantec
  17.  */
  18.  
  19. //     02/15/97    RKM    Added change checking on setter before invalidating
  20. //                    Added range checking in setValue
  21. //     02/27/97    RKM    Merged Scott's change to call invalidate in setValue
  22.  
  23. //???RKM??? Shouldn't all of these invalidate calls be repaint
  24.  
  25. public abstract class Slider
  26.     extends Canvas
  27. {
  28.     /**
  29.      * Defines the slider tick style where the tick marks appear to the left of
  30.      * the slider thumb.
  31.      */
  32.     public static final int TICK_LEFT       = 0;
  33.  
  34.    /**
  35.      * Defines the slider tick style where the tick marks appear to the right of
  36.      * the slider thumb.
  37.      */
  38.     public static final int TICK_RIGHT      = 1;
  39.  
  40.     /**
  41.      * Defines the slider tick style where the tick marks appear below
  42.      * the slider thumb.
  43.      */
  44.     public static final int TICK_BOTTOM     = 0;
  45.  
  46.     /**
  47.      * Defines the slider tick style where the tick marks appear above
  48.      * the slider thumb.
  49.      */
  50.     public static final int TICK_TOP        = 1;
  51.  
  52.     /**
  53.      * Defines the slider tick style where the tick marks appear both
  54.      * to the left and right of the slider thumb.
  55.      */
  56.     public static final int TICK_BOTH       = 2;
  57.  
  58.     /**
  59.      * Disables the display of slider tick marks.
  60.      */
  61.     public static final int TICK_NONE       = 3;
  62.  
  63.     /**
  64.      * (not used).
  65.      */
  66.     protected boolean enabled;
  67.     /**
  68.      * The current component width.
  69.      */
  70.     protected int width;
  71.     /**
  72.      * The current component height.
  73.      */
  74.     protected int height;
  75.     /**
  76.      * The current slider tick mark style.
  77.      */
  78.     protected int style;
  79.     /**
  80.      * The current tick mark display frequency.
  81.      */
  82.     protected int freq;
  83.     /**
  84.      * The minimum value of the slider range.
  85.      */
  86.     protected int min;
  87.     /**
  88.      * The maximum value of the slider range.
  89.      */
  90.     protected int max;
  91.     /**
  92.      * The position of the slider thumb last time control painted.
  93.      */
  94.     protected int prevPos;
  95.     /**
  96.      * The current position of the slider thumb.
  97.      */
  98.     protected int curPos;
  99.     /**
  100.      * Flag indicating if border should be drawn.
  101.      */
  102.     protected boolean showBorder;
  103.  
  104.     /**
  105.      * Constructs the Slider.
  106.      */
  107.     protected Slider()
  108.     {
  109.     }
  110.  
  111.     /**
  112.      * Returns the current slider tick mark style.
  113.      * @return one of TICK_LEFT, TICK_RIGHT, TICK_TOP, TICK_BOTTOM, TICK_BOTH, or TICK_NONE
  114.      */
  115.     public int getTickStyle()
  116.     {
  117.         return style;
  118.     }
  119.  
  120.     /**
  121.      * Sets the minimum value of the slider range.
  122.      * @param min the new minimum slider value
  123.      * @see #getMinValue
  124.      * @see #setMaxValue
  125.      */
  126.     public void setMinValue(int min)
  127.     {
  128.         if (this.min != min)
  129.         {
  130.             this.min = min;
  131.     
  132.             invalidate();
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * Returns the current minimum value of the slider range.
  138.      * @see #setMinValue
  139.      * @see #getMaxValue
  140.      */
  141.     public int getMinValue()
  142.     {
  143.         return min;
  144.     }
  145.  
  146.     /**
  147.      * Sets the maximum value of the slider range.
  148.      * @param max the new maximum slider value
  149.      * @see #getMaxValue
  150.      * @see #setMinValue
  151.      */
  152.     public void setMaxValue(int max)
  153.     {
  154.         if (this.max != max)
  155.         {
  156.             this.max = max;
  157.     
  158.             invalidate();
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Returns the current maximum value of the slider range.
  164.      * @see #setMaxValue
  165.      * @see #getMinValue
  166.      */
  167.     public int getMaxValue()
  168.     {
  169.         return max;
  170.     }
  171.  
  172.     /**
  173.      * Sets the tick mark display frequency.
  174.      * @param freq the range in value between tick marks
  175.      * @see #getTickFreq
  176.      */
  177.     public void setTickFreq(int freq)
  178.     {
  179.         if (this.freq != freq)
  180.         {
  181.             this.freq = freq;
  182.     
  183.             invalidate();
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * Returns the current tick mark display frequency.
  189.      * @return the range in value between tick marks
  190.      * @see #setTickFreq
  191.      */
  192.     public int getTickFreq()
  193.     {
  194.         return freq;
  195.     }
  196.  
  197.     /**
  198.      * Sets the slider value.
  199.      * @param pos the new slider value
  200.      * @see #getValue
  201.      */
  202.     public void setValue(int pos)
  203.     {
  204.         //Do range checking on pos
  205.         if (pos < getMinValue())
  206.             pos = getMinValue();
  207.         else if (pos > getMaxValue())
  208.             pos = getMaxValue();
  209.         
  210.         doMove((pos - min) / freq, false);
  211.         
  212.         repaint();
  213.     }
  214.  
  215.     /**
  216.      * Returns the current slider value.
  217.      * @see #setValue
  218.      */
  219.     public int getValue()
  220.     {
  221.         return curPos * freq + min;
  222.     }
  223.  
  224.     /**
  225.      * Sets the border display flag.
  226.      * @param f true for the border to show
  227.      * @see #getShowBorder
  228.      */
  229.     public void setShowBorder(boolean f)
  230.     {
  231.         if (showBorder != f)
  232.         {
  233.             showBorder = f;
  234.     
  235.             invalidate();
  236.         }
  237.     }
  238.  
  239.     /**
  240.      * Returns the current border display flag.
  241.      * @return true if the border is visible
  242.      * @see #setShowBorder
  243.      */
  244.     public boolean getShowBorder()
  245.     {
  246.         return showBorder;
  247.     }
  248.  
  249.     /**
  250.      * Enables this component so that it will respond to user input.
  251.      * This is a standard Java AWT method which gets called to enable 
  252.      * this component. Once enabled this component will respond to user input.
  253.      * 
  254.      * @see #disable
  255.      */
  256.     public synchronized void enable()
  257.     {
  258.         super.enable();
  259.     }
  260.  
  261.     /**
  262.      * Disables this component so that it doesn't respond to user input.
  263.      * This is a standard Java AWT method which gets called to disable
  264.      * this component. Once disabled this component will not respond to user 
  265.      * input.
  266.      *
  267.      * @see #enable
  268.      */
  269.     public synchronized void disable()
  270.     {
  271.         super.disable();
  272.     }
  273.  
  274.     /**
  275.      * Returns the recommended dimensions to properly display this component.
  276.      * This is a standard Java AWT method which gets called to determine
  277.      * the recommended size of this component. 
  278.      * @return the preferred size, in this case the latest reshape() size
  279.      *
  280.      * @see java.awt.Component#minimumSize
  281.      */
  282.     public Dimension preferredSize()
  283.     {
  284.         return new Dimension(width, height);
  285.     }
  286.  
  287.     /**
  288.      * This abstract routine updates the thumb position, paints the slider, and
  289.      * posts a new action event, as needed. If the thumb position has
  290.      * changed or the forcePost parameter is true the component will be painted
  291.      * and an action event posted.
  292.      * @param pos the new thumb position
  293.      * @param forcePost true forces a repaint and posting of an action message 
  294.      * even if the thumb position hasn't changed
  295.      */
  296.     protected abstract void doMove(int pos, boolean forcePost);
  297. }
  298.  
  299.